package com.normalsys.live.service;

import com.normalsys.live.base.config.ResponseCode;
import com.normalsys.live.base.exp.BizException;
import com.normalsys.live.base.util.token.DateUtil;
import com.normalsys.live.bean.*;
import com.normalsys.live.dao.*;
import com.normalsys.live.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created with IntelliJ IDEA.
 * User: 刘天宇
 * Date: 2018/7/16
 * Time: 20:39
 * Description:
 */
@Service
public class UserService {

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private UserFormDAO userFormDAO;



    /**
     * 详情
     * @param id
     * @return
     */
    public User detail(Long id) {
        return userDAO.selectByPrimaryKey(id);
    }


    /**
     * 添加form
     * @param userFormParamBean
     * @return
     */
    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.READ_COMMITTED)
    public UserForm addForm(UserFormParamBean userFormParamBean) {
        //查询用户是否存在
        User currentUser = userDAO.selectByPrimaryKey(userFormParamBean.getUserId());
        if(null == currentUser) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户不存在");
        }
        Long date = System.currentTimeMillis() + 7 * 24 * 60 * 60 * 100 - 2 * 60 * 60 * 1000;
        UserForm userForm = new UserForm();
        userForm.setOpenid(currentUser.getOpenid());
        userForm.setFormId(userFormParamBean.getFormId());
        userForm.setExpireTime(DateUtil.longToStrDate(date));

        userFormDAO.insert(userForm);
        return userForm;
    }

}
